//
//  UIButton+Countdown.m
//  U_Airplane
//
//  Created by iOS on 2017/11/2.
//  Copyright © 2017年 cj. All rights reserved.
//

#import "UIButton+Countdown.h"

@implementation UIButton (Countdown)

- (void)startWithTime:(NSInteger)timeLine {
    
    if(timeLine == 0){
        return;
    }
    
    self.userInteractionEnabled = NO;
    
    //倒计时时间 - 60S
    __block NSInteger timeOut = timeLine;
    DefineWeakSelf;
    //执行队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //计时器 -》 dispatch_source_set_timer自动生成
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, NSEC_PER_SEC, NSEC_PER_SEC);    dispatch_source_set_event_handler(timer, ^{
        if (timeOut <= 0) {
            dispatch_source_cancel(timer);
            //主线程设置按钮样式
            dispatch_async(dispatch_get_main_queue(), ^{
                // 倒计时结束
                NSString *title = NSLocalizedString(@"change_pwd_phone_code", @"");
                weakSelf.titleLabel.text = title;
                [weakSelf setTitle:title forState:UIControlStateNormal];
                weakSelf.userInteractionEnabled = YES;
            });
        } else {
            //开始计时
            //剩余秒数 seconds
            NSInteger hour = timeOut / (3600 * 24);
            NSInteger min = (timeOut % 3600) / 60;
            NSInteger seconds = timeOut % 60;
            
            NSString *strTime = [self timerTimeStr:hour minutes:min seconds:seconds];
            
            //主线程设置按钮样式
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1.0];
                NSString *title = NSLocalizedString(@"register_phone_code_hint", @"");
                title = [NSString stringWithFormat:@"%@%@", strTime, title];
                weakSelf.titleLabel.text = title;
                [weakSelf setTitle:title forState:UIControlStateNormal];
                [UIView commitAnimations];
            });
            timeOut--;
        }
    });
    dispatch_resume(timer);
}

#pragma mark -- 计时时间
-(NSString *)timerTimeStr:(NSInteger)hour minutes:(NSInteger)mins seconds:(NSInteger)seconds{
    NSString *hourStr = @"";
    NSString *secondStr = @"";
    NSString *minStr = @"";
    if(seconds == 0){
        if (mins > 0) {
            mins--;
            seconds = 59;
        }
    }
    if (mins == 0) {
        if (hour > 0) {
            hour--;
            mins = 59;
        }
    }
    if(hour == 0 && seconds == 0 && mins <= 0){
        hour = 0;
        mins = 0;
        seconds = 0;
    }
    if (hour < 10) {
        hourStr = @"0";
    }
    if(seconds < 10){
        secondStr = @"0";
    }
    if(mins < 10){
        minStr = @"0";
    }
    
    if(hour == 0 && mins == 0){
        return [NSString stringWithFormat:@"%@%ld",secondStr,seconds];
    }
    
    return [NSString stringWithFormat:@"%@%ld:%@%ld:%@%ld",hourStr,hour,minStr,mins,secondStr,seconds];
}

@end
